<!--  -->
<template>
  <a-upload name="file" :multiple="true" :fileList="fileList" :action="baseUrl + action" :headers="headers" @change="handleChange">
    <a-button style="margin-right:15px" size="small">
      {{ text }}
    </a-button>
  </a-upload>
</template>
<script>
// import { axios } from '@/utils/request'
import storage from 'store'
import { UPLOAD_URL } from '@/const/const'
import { ACCESS_TOKEN } from '@/store/mutation-types'
export default {

  props: {
    text: {
      type: String,
      default: '上传文件'
    },
    value: {
      type: String,
      default: ''
    },
    action: {
      type: String,
      default: UPLOAD_URL
    },
    accept: {
      type: String,
      default: 'xls'
    },
    headers: {
      type: Object,
      default: () => {
        return { Authorization: storage.get(ACCESS_TOKEN) }
      }
    },
    multiple: {
      type: Boolean,
      default: false
    },
    fileData: {
      type: Object,
      default: () => { }
    }
  },
  data () {
    return {
      a: [],
      fileList: [],
      baseUrl: process.env.VUE_APP_API_BASE_URL
    }
  },
  methods: {
    clearFile () {
      this.fileList = []
    },
    // handleUpload () {
    //   const { fileList } = this
    //   const formData = new FormData()
    //   fileList.forEach(file => {
    //     formData.append('files[]', file)
    //   })
    //   // this.uploading = true

    //   // You can use any AJAX library you like
    //   axios({
    //     url: this.action,
    //     method: 'post',
    //     processData: false,
    //     data: formData,
    //   }).then(res => {
    //     console.log(res)
    //   }).catch(() => {
    //   })
    // },
    handleChange (info) {
      console.log(info, 'info')
      const fileList = [...info.fileList]
      this.fileList = fileList
      if (info.file.status !== 'uploading') {
        console.log(info.file, info.fileList)
      }
      if (info.file.status === 'done') {
        if (info.file.response.code === 200) {
          this.$message.success('上传成功')

          this.$emit('ok', info.file.response.data)
        } else {
          this.$message.error(info.file.response.msg)
        }

        // this.$message.success(`${info.file.name} file uploaded successfully`)
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name} file upload failed.`)
      }
    },
  },
}
</script>

<style lang='less' scoped>
</style>